package com.doit.day04

import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.immutable

/**
 * @Author:
 * @WX: 17710299606
 * @Tips: 学大数据 ,到多易教育
 * @DOC: https://blog.csdn.net/qq_37933018?spm=1000.2115.3001.5343
 * @Description:
 */
object Demo03TextAcc {
  def main(args: Array[String]): Unit = {

       val  conf  = new SparkConf()
             .setMaster("local[*]")
             .setAppName(this.getClass.getSimpleName)
           val sc = SparkContext.getOrCreate(conf)

    val  ls: immutable.Seq[String] =   "a a a a b b b b c c c c"::"a a a a b b b c c c"::Nil


    val words = sc.parallelize(ls, 2).flatMap(_.split("\\s+" , -1))

    // 创建(注册)累加器
    val accumulator = new MyAccumulator()
    sc.register(accumulator , "myacc")

    words.foreach(word=>{
       // 累加
      accumulator.add(word)
    })

    // 我爱中国  我  我爱  中国  爱中国


    // 获取结果
    accumulator.value.foreach(println)



  }

}
